package com.yueya.common.action;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.basic.user.model.User;
import com.yueya.common.constant.BaseConstant;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * BaseController
 */
@Before(CommonInterceptor.class)
public class BaseController extends Controller {

	public static Map<String, Object> jsonMap = new HashMap<String, Object>();

	public int getRows() {
		int rows = 20;
		if (getPara("rows") != null) {
			rows = getParaToInt("rows");
		}
		return rows;
	}

	public int getPage() {
		int page = 1;
		if (getPara("page") != null && getParaToInt("page") != 0) {
			page = getParaToInt("page");
		}

		return page;
	}

	public String getLoginName() {
		String user = getCookie(BaseConstant.COOKIE_KEY_LOGINNAME);
		return user;
	}

	public Record getUserWithCookie() {
		String loginName = getCookie(BaseConstant.COOKIE_KEY_LOGINNAME);
		Record r = null;
		if (loginName != null && !"".equals(loginName)) {
			r = User.service.getUserByLoginName(getCookie(BaseConstant.COOKIE_KEY_LOGINNAME));
		}
		return r;
	}

	public JSONObject getJson() {
		String hr = getRequest().getHeader("token");
		System.out.println(hr);
		return JSONObject.fromObject(HttpKit.readData(getRequest()));
	}

	protected JSONArray getJsons() {
		return JSONArray.fromObject(HttpKit.readData(getRequest()));

	}

}
